package com.wt.springsamples.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wt.springsamples.annotation.AccessControl;
import com.wt.springsamples.common.ApiResult;
import com.wt.springsamples.common.BizException;
import com.wt.springsamples.model.Org;
import com.wt.springsamples.model.dto.OrgTreeNode;
import com.wt.springsamples.biz.service.IOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("组织管理")
@RestController
@RequestMapping("/api/org")
public class OrgController {

    @Autowired
    private IOrgService orgService;


    @ApiOperation(value = "org:view",nickname ="组织管理-查看")
    @GetMapping("/{id}")
    public ApiResult getById(@PathVariable Long id){

        Org org = orgService.getById(id);
        return ApiResult.ok(org);
    }

    @ApiOperation(value = "org:all",nickname ="组织管理-查询全部")
//    @PreAuthorize("hasAuthority('org:query')")
    @GetMapping("/findAll")
    public ApiResult findAll(){
        List<Org> list = orgService.list();
        return  ApiResult.ok(list);
    }

    /**
     * mybatis-plus 分页查询(需要手动配置PaginationInnerInterceptor)
     * @param pageNum
     * @param pageSize
     * @param org
     * @return
     */
    @ApiOperation(value = "org:page",nickname ="组织管理-分页查询")
//    @PreAuthorize("hasAuthority('org:query')")
    @GetMapping("/findPage")
    public ApiResult findPage(@RequestParam(required = false,defaultValue ="1") Integer pageNum
            , @RequestParam(required = false,defaultValue ="5") Integer pageSize
            , Org org){
        Page<Org> page = new Page(pageNum,pageSize);
        QueryWrapper<Org> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(org.getName())){
            wrapper.like("name",org.getName());
        }
        page = orgService.page(page, wrapper);
        return  ApiResult.ok(page);
    }

    /**
     * PageHelper 分页查询
     * @param pageNum
     * @param pageSize
     * @param org
     * @return
     */
    @GetMapping("/findPage2")
    public ApiResult findPage2(@RequestParam(required = false,defaultValue ="1") Integer pageNum
            , @RequestParam(required = false,defaultValue ="5") Integer pageSize
            , Org org){
        QueryWrapper<Org> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(org.getName())){
            wrapper.like("name",org.getName());
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Org> orgList = orgService.list(wrapper);
        PageInfo pageInfo = new PageInfo(orgList);
        return  ApiResult.ok(pageInfo);
    }

    @ApiOperation(value = "org:update",nickname ="组织管理-更新")
    @PostMapping("/update")
    public ApiResult update(@RequestBody Org org){
        if(org!=null && org.getId()!=null){
            orgService.updateById(org);
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    @ApiOperation(value = "org:deleteById",nickname ="组织管理-删除")
    @DeleteMapping("/deleteById")
    public ApiResult deleteById(Integer id){
//        UserDetailServiceImpl service = new  UserDetailServiceImpl();
        orgService.removeById(id);
        return  ApiResult.ok();
    }

    @ApiOperation(value = "org:deleteBatch",nickname ="组织管理-批量删除")
    @DeleteMapping("/batchRemove")
    public ApiResult batchRemove(@RequestBody List<Long> idList){
        boolean flag = orgService.removeByIds(idList);
        if(flag){
            return ApiResult.ok();
        }else{
            return ApiResult.fail();
        }
    }

    @ApiOperation(value = "org:treeNode",nickname ="组织管理-树形节点")
    @GetMapping("/treeNode/{orgId}")
    public ApiResult treeNodeInfo(@PathVariable("orgId") long orgId){
        OrgTreeNode treeNode = orgService.getOrgTreeNodeInfo(orgId);
        return ApiResult.ok(treeNode);
    }

    @GetMapping("/treeNodeList")
//    @ApiOperation(value = "org:tree",nickname ="组织管理-树形列表")
//    @PreAuthorize("hasAuthority('org:tree')")
    @AccessControl(value="hasAuthority('org:tree')",code ="org:tree",name="组织管理-树形列表")
    public ApiResult treeNodeList(){
        List<OrgTreeNode> treeNodeList  = orgService.getOrgTreeNodeList();
        return ApiResult.ok(treeNodeList);
    }

}
